package com.aperos;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class BarErrorEdit extends Activity implements Runnable {
	Context context;
	
	private EditText barErrorMessage;
	private RadioButton barNotExist;
	private RadioButton barBadLocation;
	private RadioButton barOther;
	private ProgressDialog dialog;

	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bar_error_edit);
		context = this;
		
		barErrorMessage = (EditText) findViewById(R.id.bar_error_edit_message);
		barNotExist = (RadioButton) findViewById(R.id.bar_error_not_exist);
		barBadLocation = (RadioButton) findViewById(R.id.bar_error_bad_location);
		barOther = (RadioButton) findViewById(R.id.bar_error_other);
		
		barNotExist.setChecked(true);
		
		
		final Button button = (Button) findViewById(R.id.bar_error_btn_save);
        button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog = ProgressDialog.show(context, "", getString(R.string.dialog_save_bar), true);
				new Thread(BarErrorEdit.this).start();
				
				
				
			}
           
        });
	}
	
	@Override
	public void run() {
		try {
			int refError = 0;
			if (barNotExist.isChecked()) {
				refError = 0;
			} else if (barBadLocation.isChecked()) {
				refError = 1;
			} else if (barOther.isChecked()) {
				refError = 2;
			}
			
			String message = null;
			if (!"".equals(barErrorMessage.getText().toString())) {
				message = barErrorMessage.getText().toString();
			}
			
			Service.addBarError(Service.currentBar.getId(), refError, message, context);
			
			Intent myIntent = new Intent(context, AperosPasChers.class);
			context.startActivity(myIntent);
		} catch (Exception e) {
			e.printStackTrace();
			mHandler.post(mUpdateResults);
		}
	}
	
	final Handler mHandler = new Handler();

    final Runnable mUpdateResults = new Runnable() {
        public void run() {
        	dialog.dismiss();
        	displayError();
        }
    };
    
	public void displayError() {
		Toast.makeText(context, R.string.error_save_bar_error, 2000).show();
	}

	
	
}
